<template>
  <el-dialog v-model="state.dialogVisible" :before-close="handleClose" width="25%">
    <template #header>
      <div class="my-header">
        <div class="title-hr"></div>
        <div>
          <span class="title-top">智能水文预报设置</span>
        </div>
      </div>
    </template>
    <div class="dialog-height">
      <div class="base-title">模型信息</div>
      <div class="table-box">
        <el-table
          :data="state.SectionsList"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          ref="tableRef"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="预报对象" align="center" prop="blknm" />
        </el-table>
      </div>
    </div>
    <div class="bottom-box">
      <el-button type="info" @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSave">保存</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
// ai洪水断面信息表
import { getFloodSectionsList } from '@/services/modules/model-config.js'
import { ref, reactive, defineExpose, nextTick } from 'vue'
const state = reactive({
  dialogVisible: false,
  SectionsList: [],
  blkcdArr: []
})
const tableRef = ref(null)
async function handleOpenSetting(zoneId, secArr) {
  state.dialogVisible = true
  await acceptList(zoneId, secArr)
  let blkcdData = []
  let newArr = []
  secArr.forEach((e) => {
    blkcdData.push(e.blkcd)
  })

  newArr = state.SectionsList.filter((b) => {
    if (blkcdData.includes(b.blkcd)) {
      return b
    }
  })

  if (newArr.length) {
    newArr.forEach((a) => {
      tableRef.value.toggleRowSelection(a, true)
    })
  }
}
function handleClose() {
  state.dialogVisible = false
}
// 选择多行
function handleSelectionChange(arr) {
  state.blkcdArr = []
  arr.forEach((e) => {
    state.blkcdArr.push({ blkcd: e.blkcd })
  })
}
function acceptList(wId, secArr) {
  return new Promise((resolve, reject) => {
    getFloodSectionsList({ zoneId: wId }).then((res) => {
      if (res.code == 200) {
        state.SectionsList = res.data
        if (!secArr.length) {
          nextTick(() => {
            state.SectionsList.forEach((e) => {
              tableRef.value.toggleRowSelection(e, true)
            })
          })
        }

        resolve(state.SectionsList)
      }
    })
  })
}

const emit = defineEmits(['saveListBlkcd'])
// 保存模型信息
function handleSave() {
  state.dialogVisible = false

  emit('saveListBlkcd', state.blkcdArr)
}
// 取消
function handleCancel() {
  state.dialogVisible = false
  tableRef.value.clearSelection()
}
defineExpose({ handleOpenSetting })
</script>

<style lang="less" scoped>
.my-header {
  display: flex;
  align-items: center;
  padding-bottom: 5px;
  height: 35px;
  border-bottom: 2px solid #eee;
  font-size: 21px;
  .title-hr {
    height: 18px;
    border: #4fc0f1 2px solid;
    margin-right: 10px;
  }
  .title-top {
    color: #4fc0f1;
  }
}
.dialog-height {
  height: 50vh;
  .base-title {
    height: 30px;
    color: #169bd5;
    margin-bottom: 10px;
  }
  .table-box {
    height: calc(100% - 40px);
    :deep(.el-table--fit) {
      height: 100%;
    }
  }
}
.bottom-box {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 50px;
  width: 100%;
}
</style>
